package redisson.rest.topic;

import java.util.Map;

import org.redisson.api.RPatternTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class TopicConsumer implements SmartInitializingSingleton {

	@Autowired
	RedissonClient redissonClient;

	@Override
	public void afterSingletonsInstantiated() {
		////////////////////// 必须先配置监听//////////////////////
		log.info("### 监听redisson消息,启动成功,,, ");
		// 通配符订阅一定要用 RPatternTopic
		RPatternTopic patternTopic = redissonClient.getPatternTopic("test:topic:*");
		patternTopic.addListener(Map.class, (pattern, channel, msg) -> {
			log.info(">>>> 收到 pattern={} channel={} msg={}", pattern, channel, msg);
		});
		log.info("### 模式订阅已建立");

	}

}
